package rss;

import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.UnknownHostException;

import com.sun.syndication.feed.synd.SyndFeed;
import com.sun.syndication.io.FeedException;
import com.sun.syndication.io.SyndFeedInput;
import com.sun.syndication.feed.synd.SyndEntryImpl;
import com.sun.syndication.io.XmlReader;

import java.util.List;
import java.util.ArrayList;

public class News {

	/**
	 * @author Igor Ruda Alves Neves
	 * @version 1.0
	 */

	private String url;
	private URL feedUrl;
	private SyndFeedInput input;
	private SyndFeed feed;
	private List<String> newsList;
	private int count = 0; // causes not repeat the news returned.

	/**
	 * News constructor. Read RSS.
	 * 
	 * @param url
	 *            - URL of the news.
	 */
	public News(String url) throws MalformedURLException,
			IllegalArgumentException, FeedException, IOException,
			UnknownHostException, NullPointerException {
		// Initializes the feed
		if (url == null) {
			throw new NullPointerException("URL Invalida!");
		} else {
			this.url = url;
		}

		newsList = new ArrayList<String>();
		feedUrl = new URL(url);
		input = new SyndFeedInput();
		feed = input.build(new XmlReader(feedUrl));

		addNewsInList();

	}

	/**
	 * Returns URL.
	 * 
	 * @return url - The current URL
	 * 
	 */
	public String getRss() {
		return url;
	}

	/**
	 * Sets new URL.
	 * 
	 * @param newUrl
	 *            - The new URL
	 * @throws Exception
	 */
	public void setRss(String newUrl) throws Exception {
		if (newUrl == null || newUrl.equals("")) {
			throw new Exception("URL Invalida!");
		} else {
			url = newUrl;
		}
	}

	/**
	 * Returns a list of String type
	 * 
	 * @return newsList - List containing the news title.
	 */
	public List<String> getAllNews() {
		return newsList;
	}

	/**
	 * Add news to a list.
	 */
	private void addNewsInList() {
		newsList = new ArrayList<String>();
		SyndEntryImpl elements = null;
		try {
			for (Object entry : feed.getEntries()) {
				elements = (SyndEntryImpl) entry;
				newsList.add(elements.getTitle());
			}
		} catch (Exception e) {
			System.err.println(e.getMessage());
		}
	}

	/**
	 * Update the News.
	 * 
	 * @return true - If the method might refresh. false - If the method can't
	 *         established a connection with the host else if occured other
	 *         problem with the RSS Feed.
	 */
	public boolean updateNews() {
		// Refresh news' List
		SyndEntryImpl newsObject = null;

		for (Object entry : feed.getEntries()) {
			newsObject = (SyndEntryImpl) entry;
			if (getAllNews().get(0) == newsObject.getTitle()) {
				return false;
			}
		}

		addNewsInList();

		return true;
	}

	/**
	 * Returns a news title
	 * 
	 * @return newsTitle - the news title that is contained in the newsList
	 * @throws Exception
	 */
	public String getANewsTitle() {

		String newsTitle = newsList.get(count);
		if (count == newsList.size() - 1) {
			updateNews();
			count = 0;
			return newsTitle;
		} else {
			count++;
			return newsTitle;
		}

	}

	/**
	 * Returns the Title of feed.
	 * 
	 * @return feed.getTitle() - The Feed's Title of Type String
	 */
	public String getFeedTitle() {
		return feed.getTitle();
	}

}